// admin/wordbooks/wordbooks.js
Page({
  data: {
    wordbooksList: [],
    loading: true,
    page: 1,
    hasMore: true
  },

  onLoad: function() {
    this.fetchWordbooksList();
  },

  fetchWordbooksList: function() {
    // 模拟获取词书列表数据
    setTimeout(() => {
      const newWordbooks = [];
      for (let i = 0; i < 10; i++) {
        const id = (this.data.page - 1) * 10 + i + 1;
        newWordbooks.push({
          id: id,
          title: '英语单词书 ' + id,
          author: '作者' + (Math.floor(Math.random() * 20) + 1),
          wordCount: Math.floor(Math.random() * 1000) + 500,
          createDate: '2023-0' + (Math.floor(Math.random() * 9) + 1) + '-' + (Math.floor(Math.random() * 28) + 1),
          status: Math.random() > 0.5 ? '已发布' : '未发布'
        });
      }

      this.setData({
        wordbooksList: this.data.page === 1 ? newWordbooks : [...this.data.wordbooksList, ...newWordbooks],
        loading: false,
        hasMore: this.data.page < 5 // 模拟只有5页数据
      });
    }, 1000);
  },

  // 加载更多
  loadMore: function() {
    if (!this.data.loading && this.data.hasMore) {
      this.setData({
        page: this.data.page + 1,
        loading: true
      });
      this.fetchWordbooksList();
    }
  },

  // 查看词书详情
  viewWordbookDetail: function(e) {
    const wordbookId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/admin/wordbooks/detail?id=' + wordbookId
    });
  },

  // 添加新词书
  addWordbook: function() {
    wx.navigateTo({
      url: '/pages/admin/wordbooks/add'
    });
  }
});